
package com.nuoson.p3c.pmd.lang.java.rule.naming;

import com.nuoson.p3c.pmd.I18nResources;
import com.nuoson.p3c.pmd.lang.AbstractXpathRule;
import com.nuoson.p3c.pmd.lang.java.util.ViolationUtils;

import net.sourceforge.pmd.lang.ast.Node;

/**
 * [Mandatory] Brackets are a part of an Array type. The definition could be:
 * String[] args;
 * 数组的声明应该在类型处使用[]，而不是变量处。例如：String[] args, 而不是 String args[]。
 * TODO: 后续可以添加 String[] args[]; 这种情况的检测
 */
public class ArrayNamingShouldHaveBracketRule extends AbstractXpathRule {
    private static final String XPATH = "//VariableDeclaratorId[(@ArrayType='true') and (../../Type[@ArrayType='false'])]";

    public ArrayNamingShouldHaveBracketRule() {
        setXPath(XPATH);
    }

    @Override
    public void addViolation(Object data, Node node, String arg) {
        ViolationUtils.addViolationWithPrecisePosition(
                this,
                node,
                data,
                I18nResources.getMessage(
                        "java.naming.ArrayNamingShouldHaveBracketRule.violation.msg",
                        node.getImage()));
    }
}
